<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件上传</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        .upload-container {
            border: 2px dashed #ccc;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
        }
        .upload-container h2 {
            color: #333;
        }
        .file-input {
            margin: 20px 0;
        }
        .file-input input {
            display: none;
        }
        .file-input label {
            display: inline-block;
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .file-input label:hover {
            background-color: #45a049;
        }
        .upload-icon {
            font-size: 48px;
            margin: 20px 0;
            display: none;
        }
        .success-icon {
            color: #4CAF50;
            display: none;
        }
        .error-icon {
            color: #f44336;
            display: none;
        }
        .loading-icon {
            animation: spin 2s linear infinite;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .upload-status {
            margin-top: 10px;
            font-size: 0.9em;
            color: #666;
        }
        .file-actions {
            margin-top: 20px;
        }
        .file-actions a, .file-actions button {
            margin: 0 10px;
            padding: 5px 10px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        .file-actions a {
            text-decoration: none;
            color: white;
        }
        .preview-btn {
            background-color: #2196F3;
        }
        .download-btn {
            background-color: #4CAF50;
        }
        .delete-btn {
            background-color: #f44336;
        }
    </style>
</head>
<body>
<div class="upload-container" id="app">
    <h2>文件上传</h2>
    <div class="file-input">
        <input type="file" id="file" @change="handleFileChange">
        <label for="file"><i class="fas fa-paperclip"></i> 选择文件</label>
    </div>
    <div class="upload-icon" id="uploadIcon">
        <i class="fas fa-spinner loading-icon"></i>
        <i class="fas fa-check-circle success-icon"></i>
        <i class="fas fa-exclamation-circle error-icon"></i>
    </div>
    <div class="upload-status" id="uploadStatus"></div>
    <button @click="uploadFile" id="uploadButton" style="display: none;">开始上传</button>
    <div class="file-actions" v-if="uploadedFile">
        <a :href="uploadedFile.fileUrl" class="preview-btn" target="_blank">预览</a>
        <a :href="uploadedFile.fileUrl" class="download-btn" download>下载</a>
        <a href="javascript:void(0)" class="delete-btn" @click="deleteFile">删除</a>
    </div>
</div>

<!-- 引入 Vue 和 Axios -->
<script src="/assets/vue/vue.js"></script>
<script src="/assets/axios/axios.min.js"></script>

<script>
    new Vue({
        el: '#app',
        data: {
            file: null,
            isUploading: false,
            uploadSuccess: false,
            uploadError: false,
            uploadedFile: null
        },
        methods: {
            handleFileChange(event) {
                this.file = event.target.files[0];
                document.getElementById('uploadButton').style.display = 'inline-block';
                // 每次选择文件后，清空文件输入框的值，以便下次选择相同文件时触发 change 事件
                event.target.value = null;
            },
            uploadFile() {
                if (!this.file) {
                    alert('请选择文件后再上传');
                    return;
                }

                this.isUploading = true;
                document.getElementById('uploadIcon').style.display = 'block';
                document.getElementById('uploadButton').style.display = 'none';
                document.getElementById('uploadStatus').textContent = '正在上传...';

                // 显示加载图标，隐藏成功和错误图标
                document.querySelector('.loading-icon').style.display = 'inline';
                document.querySelector('.success-icon').style.display = 'none';
                document.querySelector('.error-icon').style.display = 'none';

                const formData = new FormData();
                formData.append('file', this.file);

                axios.post('/api/file/upload', formData, {
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                })
                    .then(response => {
                        this.isUploading = false;
                        this.uploadSuccess = true;

                        // 显示成功图标，隐藏加载图标
                        document.querySelector('.loading-icon').style.display = 'none';
                        document.querySelector('.success-icon').style.display = 'inline';
                        document.getElementById('uploadStatus').textContent = '上传成功!';

                        // 保存上传的文件信息
                        this.uploadedFile = response.data.data;

                        console.log('文件上传成功:', response.data);
                    })
                    .catch(error => {
                        this.isUploading = false;
                        this.uploadError = true;

                        // 显示错误图标，隐藏加载图标
                        document.querySelector('.loading-icon').style.display = 'none';
                        document.querySelector('.error-icon').style.display = 'inline';
                        document.getElementById('uploadStatus').textContent = '上传失败: ' + (error.response && error.response.data && error.response.data.message ? error.response.data.message : '未知错误');

                        console.error('文件上传失败:', error);
                    });
            },
            deleteFile() {
                if (confirm('确定要删除文件吗？')) {
                    axios.delete('/api/file/delete/'+this.uploadedFile.filename)
                        .then(response => {
                            // console.log('文件删除成功:', response.data);
                            if (response.data.code===200) {

                                document.getElementById('uploadStatus').textContent = '文件删除成功!';
                                alert('文件删除成功');
                                this.uploadedFile = null;
                                this.uploadSuccess = false;
                                document.getElementById('uploadStatus').textContent = '';
                                document.querySelector('.success-icon').style.display = 'none';
                            } else {
                                alert('文件删除失败: ' + response.data.message);
                            }
                        })
                        .catch(error => {
                            console.error('文件删除失败:', error);
                            alert('文件删除失败: ' + (error.response && error.response.data && error.response.data.message ? error.response.data.message : '未知错误'));
                        });
                }
            }
        }
    });
</script>
</body>
</html>